package com.mangosteen.app.dao.mapper;

import com.mangosteen.app.dao.provider.ItemSQLProvider;
import com.mangosteen.app.model.dao.Item;
import com.mangosteen.app.model.vo.ItemQueryParam;
import java.util.List;
import org.apache.ibatis.annotations.*;

@Mapper
public interface ItemMapper {
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("INSERT INTO ms_item(user_id,amount,description,type,status,happen_at)"
            + "VALUES (#{userId},#{amount},#{description},#{type},#{status},#{happenAt})")
    int insertItem(Item item);

    @Select("SELECT id,user_id,amount,description,type,status,happen_at FROM ms_item WHERE id= #{id}")
    @Results({
            @Result(column = "id" , property = "id"),
            @Result(column = "user_id" , property = "userId"),
            @Result(column = "amount" , property = "amount"),
            @Result(column = "type" , property = "type"),
            @Result(column = "description" , property = "description"),
            @Result(column = "happen_at" , property = "happenAt"),
            @Result(column = "status" , property = "status"),
            @Result(column = "id" ,javaType = List.class, property = "tagIds",
                many = @Many(select = "com.mangosteen.app.dao.mapper.ItemTagMappingMapper.getTagIdsByItemId" )
            )
    })
    Item getItemById(@Param("id") Long id);

    @UpdateProvider(type = ItemSQLProvider.class, method = "updateItem")
    int updateItem(Item itemToUpdate);

    @SelectProvider(type=ItemSQLProvider.class,method = "selectItemsByFilters")
    @Results(
        {
            @Result(column = "id" , property = "id"),
            @Result(column = "user_id" , property = "userId"),
            @Result(column = "amount" , property = "amount"),
            @Result(column = "type" , property = "type"),
            @Result(column = "description" , property = "description"),
            @Result(column = "happen_at" , property = "happenAt"),
            @Result(column = "status" , property = "status"),
            @Result(column = "id" ,javaType = List.class, property = "tagIds",
                many = @Many(select = "com.mangosteen.app.dao.mapper.ItemTagMappingMapper.getTagIdsByItemId" )
            )
        }
    )
    List<Item> selectItemsByFilters(ItemQueryParam param);
}
